package com.iddaa.web.resource;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.iddaa.core.domain.Person;
import com.iddaa.core.service.PersonService;
import com.iddaa.web.util.Response;

@Component
@Scope("request")
@Path("/persons")
@Produces(MediaType.APPLICATION_JSON)
public class PersonResource {

	@Autowired
	private PersonService personService;

	@GET
	@Path("/single/{id}")
	public Response load(@PathParam("id") Long id) {
		return new Response(personService.findById(id));
	}

	@GET
	@Path("/all/")
	public Response loadAll() {
		return new Response(personService.loadAll());
	}

	@DELETE
	@Path("/delete/{id}")
	public Response delete(@PathParam("id") Long id) {
		personService.deletePerson(id);
		return new Response(true);
	}

	@POST
	@Path("/save/")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response saveOrUpdate(Person person) {
		personService.savePerson(person);
		return new Response(true);
	}

}
